
package cn.edu.scau.cmi.hombio.dao;

import cn.edu.scau.cmi.hombio.domain.TaskType;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage TaskType entities.
 * 
 */
@Repository("TaskTypeDAO")

@Transactional
public class TaskTypeDAOImpl extends AbstractJpaDao<TaskType> implements TaskTypeDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] {
			TaskType.class }));

	/**
	 * EntityManager injected by Spring for persistence unit pig
	 *
	 */
	@PersistenceContext(unitName = "pig")
	private EntityManager entityManager;

	/**
	 * Instantiates a new TaskTypeDAOImpl
	 *
	 */
	public TaskTypeDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findTaskTypeByPrimaryKey
	 *
	 */
	@Transactional
	public TaskType findTaskTypeByPrimaryKey(Integer id) throws DataAccessException {

		return findTaskTypeByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findTaskTypeByPrimaryKey
	 *
	 */

	@Transactional
	public TaskType findTaskTypeByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findTaskTypeByPrimaryKey", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.TaskType) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findTaskTypeByMemo
	 *
	 */
	@Transactional
	public Set<TaskType> findTaskTypeByMemo(String memo) throws DataAccessException {

		return findTaskTypeByMemo(memo, -1, -1);
	}

	/**
	 * JPQL Query - findTaskTypeByMemo
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<TaskType> findTaskTypeByMemo(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTaskTypeByMemo", startResult, maxRows, memo);
		return new LinkedHashSet<TaskType>(query.getResultList());
	}

	/**
	 * JPQL Query - findTaskTypeByMemoContaining
	 *
	 */
	@Transactional
	public Set<TaskType> findTaskTypeByMemoContaining(String memo) throws DataAccessException {

		return findTaskTypeByMemoContaining(memo, -1, -1);
	}

	/**
	 * JPQL Query - findTaskTypeByMemoContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<TaskType> findTaskTypeByMemoContaining(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTaskTypeByMemoContaining", startResult, maxRows, memo);
		return new LinkedHashSet<TaskType>(query.getResultList());
	}

	/**
	 * JPQL Query - findTaskTypeById
	 *
	 */
	@Transactional
	public TaskType findTaskTypeById(Integer id) throws DataAccessException {

		return findTaskTypeById(id, -1, -1);
	}

	/**
	 * JPQL Query - findTaskTypeById
	 *
	 */

	@Transactional
	public TaskType findTaskTypeById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findTaskTypeById", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.TaskType) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findTaskTypeByNameContaining
	 *
	 */
	@Transactional
	public Set<TaskType> findTaskTypeByNameContaining(String name) throws DataAccessException {

		return findTaskTypeByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findTaskTypeByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<TaskType> findTaskTypeByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTaskTypeByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<TaskType>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllTaskTypes
	 *
	 */
	@Transactional
	public Set<TaskType> findAllTaskTypes() throws DataAccessException {

		return findAllTaskTypes(-1, -1);
	}

	/**
	 * JPQL Query - findAllTaskTypes
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<TaskType> findAllTaskTypes(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllTaskTypes", startResult, maxRows);
		return new LinkedHashSet<TaskType>(query.getResultList());
	}

	/**
	 * JPQL Query - findTaskTypeByName
	 *
	 */
	@Transactional
	public Set<TaskType> findTaskTypeByName(String name) throws DataAccessException {

		return findTaskTypeByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findTaskTypeByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<TaskType> findTaskTypeByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTaskTypeByName", startResult, maxRows, name);
		return new LinkedHashSet<TaskType>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(TaskType entity) {
		return true;
	}
}
